package org.example.proxy;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @version: java version 23
 * @Author: 韩学新
 * @description:
 * @date: 2025-05-21 上午11:32
 */
@Component
@Aspect
public class UserProxy {

    /**
     * 统一切入点
     */
    @Pointcut(value = "execution(* org.example.service.UserService.*(..))")
    private void pointCut() {}

    /**
     * 拦截UserService 所有方法
     * value = "execution(* org.example.service.UserService.*(..))"
     */
    @Before(value = "pointCut()")
    public void before(JoinPoint joinPoint) {

        Object[] args = joinPoint.getArgs();
        System.out.println("before：使用前置通知输出调用目标方法参数值args：" + args);
        System.out.println("目标方法执行前.........");
    }

    @After(value = "execution(* org.example.service.UserService.*(..))")
    public void after() {
        System.out.println("目标方法执行后.........");
    }

    @Around(value = "execution(* org.example.service.UserService.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知调用目标方法执行前.........");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕通知调用目标方法执行后.........");
        return proceed;
    }

    @AfterThrowing(value = "execution(* org.example.service.UserService.*(..))")
    public void afterThrowing() {
        System.out.println("异常通知");
    }
}
